package string

// 字符串的最长公共前缀
// 直接遍历即可
func LongestCommonPrefix(strs []string) string {
	if len(strs) == 0 {
		return ""
	}

	// 得到所有字符串中的最小长度，作为遍历下标
	minLen := len(strs[0])
	for _, currentStr := range strs {
		if minLen > len(currentStr) {
			minLen = len(currentStr)
		}
	}

	index := 0
	maxCommonLen := 0
	for ; index < minLen; index++ {
		toCompareChar := strs[0][index]
		isAllSame := true
		for _, currentStr := range strs {
			if currentStr[index] != toCompareChar {
				isAllSame = false
				break
			}
		}
		if !isAllSame {
			break
		}
		maxCommonLen++
	}
	return strs[0][:maxCommonLen]
}
